import Vue from 'vue'
import Vuex from 'vuex'
import { getLayoutTabsStatus, setLayoutTabsStatus } from '@/utils'

import { loadDicCodeByDictionaryEncoList } from '@/api/sysDictionaryEnco'
import { getListEnterprise } from '@/api/enterprose'

import user from '@/store/user'
import tagsView from '@/store/tags-view'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    showLayoutTabs: !!getLayoutTabsStatus(),
    logoTitle: '天飞能耗服务平台',
    isCollapsed: false,
    version: process.env.VUE_APP_VERSION,

    // 能源企业所属行业类别
    energyIndustry: [],
    // 能源种类（如：水、电、气、其他能源）
    energyVariety: [],
    // TODO: 能源计量单位
    energyUnits: [
      { key: 'water', value: '吨', unit: 't' },
      { key: 'electric', value: '千瓦时', unit: 'kwh' },
      { key: 'gas', value: '立方米', unit: 'm³' },
      { key: 'other', value: '标准煤', unit: 'tce' },
    ],
    // 入驻系统的能耗企业列表（名称和ID）
    energyEnterpriseList: [],
  },
  getters: {
    otherEnergyName (state) {
      const energyVariety = state.energyVariety
      const target = energyVariety.find(v => v.value === 'other')
      return target ? target.name : '其他能源'
    },
  },
  mutations: {
    SET_SHOW_LAYOUT_TABS (state, payload) {
      state.showLayoutTabs = payload
      setLayoutTabsStatus(payload ? 1 : 0)
    },
    SET_LOGO_TITLE (state, payload) {
      state.logoTitle = payload
    },
    SET_IS_COLLAPSED (state, payload) {
      state.isCollapsed = payload
    },
    SET_ENERGY_VARIETY (state, payload) {
      state.energyVariety = [...payload]
    },
    SET_ENERGY_INDUSTRY (state, payload) {
      state.energyIndustry = [...payload]
    },
    SET_ENERGY_ENTERPRISE_LIST (state, payload) {
      state.energyEnterpriseList = [...payload]
    },
  },
  actions: {
    GET_ENERGY_VARIETY ({ commit, state }) {
      const energyVariety = state.energyVariety
      if (!energyVariety.length) {
        loadDicCodeByDictionaryEncoList('NENG_YUAN_ZHONG_LEI').then(res => {
          if (res.code === 200) {
            const data = res.data
            commit('SET_ENERGY_VARIETY', data.map(v => ({
              name: v.dicValue,
              value: v.dicKey
            })))
          }
        })
      }
    },
    GET_ENERGY_INDUSTRY ({ commit, state }) {
      const energyIndustry = state.energyIndustry
      if (!energyIndustry.length) {
        loadDicCodeByDictionaryEncoList('HANG_YE_LEI_XING').then(res => {
          if (res.code === 200) {
            const data = res.data
            commit('SET_ENERGY_INDUSTRY', data.map(v => ({
              name: v.dicValue,
              value: v.dicKey
            })))
          }
        })
      }
    },
    GET_ENTERPRISE_LIST ({ commit, state }) {
      const lists = state.energyEnterpriseList
      if (!lists.length) {
        getListEnterprise().then(res => {
          if (res.code === 200) {
            commit('SET_ENERGY_ENTERPRISE_LIST', res.data.map(v => ({
              name: v.enterpriseName,
              value: v.id,
            })))
          }
        })
      }
    },
  },
  modules: {
    user,
    tagsView
  }
})
